package com.mtt.erp.goods.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.google.common.collect.Lists;
import com.mtt.erp.common.utils.MttErpUtil;
import com.mtt.erp.common.utils.UUIDUtils;
import com.mtt.erp.file.FileConfig;
import com.mtt.erp.file.service.GoodsFileInfoService;
import com.mtt.erp.goods.domain.MabFile;
import com.mtt.erp.goods.dao.MabFileMapper;
import com.mtt.erp.goods.dto.MabFileCreateDto;
import com.mtt.erp.goods.dto.MabFileTaskDto;
import com.mtt.erp.goods.service.IMabFileService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.mtt.erp.goods.vo.MabFileVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * @Description: 商品子表的ServiceImpl
 * @Author: guomengzhou
 * @CreateDate: 2019/6/3$ 13:40$
 * @Version: 1.0
 */
@Slf4j
@Service
public class MabFileServiceImpl extends ServiceImpl<MabFileMapper, MabFile> implements IMabFileService {
    @Value("${erp.max.batch.insert.num}")
    private int batchInsertMaxNum;
    @Autowired
    GoodsFileInfoService goodsFileInfoService;

    @Override
    public List<MabFileVo> findMabFileListByMab01(String mab01) {
        LambdaQueryWrapper<MabFile> queryWrapper = new LambdaQueryWrapper<>();
        if (StringUtils.isNotBlank(mab01)) {
            queryWrapper.eq(MabFile::getMab01, mab01);
        }else{
            return Lists.newArrayList();
        }
        List<MabFileVo> mabFileVoList = Lists.newArrayList();//封装对象
        List<MabFile>mabFileList = this.list(queryWrapper);
        mabFileList.forEach(mabFile -> {
            MabFileVo mabFileVo = MabFileVo.builder().build();
            mabFile.setMab04(FileConfig.goodImageDomain+mabFile.getMab04());
            BeanUtils.copyProperties(mabFile,mabFileVo);
            mabFileVo.setKey(mabFile.getMab00());//将主键作为key值
            mabFileVoList.add(mabFileVo);
        });
        return mabFileVoList;
    }


    @Override
    @Transactional
    public void createMabFile(MabFileCreateDto mabCreateDto) {
        MabFile mabFile = MabFile.builder().build();
        BeanUtils.copyProperties(mabCreateDto,mabFile);//将参数拷贝到对象里
        mabFile.setMab00(UUIDUtils.getUUID());//自动生成商品子表的主键
        mabFile.setMabcdate(LocalDateTime.now());
        //商品是属于哪一个用户的
        Long currentUserId = MttErpUtil.getCurrentUser().getUserId();
        mabFile.setMabcuser(currentUserId.toString());
        mabFile.setMab20(mabFile.getMab00());//映射SKU 默认商品子ID -预留不显示
        this.save(mabFile);
    }


    /**
     * 删除子商品
     *
     * @param mab00s
     */
    @Override
    @Transactional
    public void deleteMabFile(String[] mab00s) {
        List<String> list = Arrays.asList(mab00s);
        removeByIds(list);
    }

    /**
     * 定时任务使用
     * 查询所有主商品中sku列表
     * @return
     */
    public List<MabFileTaskDto> getMabList(){
        List<MabFileTaskDto> maaFileTaskDtos = Lists.newArrayList();
        List<MabFile> list = this.list();
        for (MabFile mabFile : list) {
            MabFileTaskDto maaFileTaskDto = MabFileTaskDto.builder().build();
            BeanUtils.copyProperties(mabFile,maaFileTaskDto);
            maaFileTaskDtos.add(maaFileTaskDto);
        }
        return maaFileTaskDtos;
    }

}
